package core.tool.common;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

public class StringHelper {
	
	public static boolean isEmpty(String s) {
		if((null==s) || (s.equals(""))) {
			return true;
		}
		return false;
	}	
	
	public static List<Object> split(String s,String split) {
		int fromIndex=0;
		int index=0;
		List<Object> result=new ArrayList<Object>();
		List<Integer> list=new ArrayList<Integer>();
		while(index!=-1) {
			list.add(index);
			index=s.indexOf(split,fromIndex);
			fromIndex=index+split.length();
		}
		for(int i=0;i<(list.size()-1);i++) {
			int begin=(i==0)?list.get(i):list.get(i)+split.length();
			int end=list.get(i+1);
			String item=s.substring(begin, end);
			result.add(item);
		}
		
		if(s.endsWith(split)) {
			result.add("");
		}else {
			result.add(s.substring((list.get(list.size()-1)+split.length()),s.length()));
		}
		return result;
	}		
	
	public static String getUUID(){
		String tempId1 = UUID.randomUUID().toString();
        String strId = tempId1.substring(0,8)+tempId1.substring(9,13)+tempId1.substring(14,18)+tempId1.substring(19,23)+tempId1.substring(24); 
        return strId;
	}	
	
	public static void writeStrToResponse(HttpServletResponse response,String content) {
		try {			
			response.setContentType("text/html;charset=utf-8");
			response.setCharacterEncoding("utf-8");
			response.getWriter().write(content);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	public static String getFirstUpper(String s) {
		s=s.substring(0,1).toUpperCase()+s.substring(1);
		return s;
	}
	
}
